package org.jeecg.modules.flowable.common.util.tree;

import org.jeecg.modules.flowable.common.util.validation.AssertValid;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Title:  <br>
 * Desc:  <br>
 * Date: 2020-02-03 23:58 <br>
 * Company: 联通（辽宁）产业互联网有限公司 <br>
 *
 * @author zxd
 * @version 1.0.0
 */
public class TreeDescUtil {
    private static List<TreeDesc> getTD(String parentId, TreeDesc parentObj, List<Map<String, Object>> data, String parentColName, String idColName , String colLabel, Boolean argIsContainsData, List<String> argSelectIds)
    {
        List<TreeDesc> arr=new ArrayList<>();
        if(data==null || data.size()<1)
            return arr;
        for(int i=0;i<data.size();i++)
        {
            Map cd=data.get(i);
            if(parentId == null || parentId.trim().equals(""))
            {
                if(cd.get(parentColName)==null || cd.get(parentColName).toString().trim().equals(""))
                {
                    TreeDesc td=new TreeDesc();
                    td.setValue(cd.get(idColName)+"");
                    td.setLabel(cd.get(colLabel).toString());
                    if(argSelectIds!=null && argSelectIds.contains(cd.get(idColName)+""))
                        td.setCheckbox(true);
                    else
                        td.setCheckbox(false);
                    if(argIsContainsData)
                        td.setData(cd);
                    List<TreeDesc> childrenArray=getTD(cd.get(idColName).toString().trim(),td,data,parentColName,idColName,colLabel,argIsContainsData,argSelectIds);
                    if(childrenArray!=null && childrenArray.size()>0)
                        td.setChildren(childrenArray);
                    else
                        td.setChildren(new ArrayList<>());
                    arr.add(td);
                }
            }
            else
            {
                if(cd.get(parentColName).toString().equals(parentId.toString().trim()))
                {
                    TreeDesc td=new TreeDesc();
                    td.setValue(cd.get(idColName)+"");
                    td.setLabel(cd.get(colLabel).toString());
                    if(argSelectIds!=null && argSelectIds.contains(cd.get(idColName)+""))
                        td.setCheckbox(true);
                    else
                        td.setCheckbox(false);
                    if(argIsContainsData)
                        td.setData(cd);
                    List<TreeDesc> childrenArray=getTD(cd.get(idColName).toString().trim(),td,data,parentColName,idColName,colLabel,argIsContainsData,argSelectIds);
                    if(childrenArray!=null && childrenArray.size()>0)
                        td.setChildren(childrenArray);
                    else
                        td.setChildren(new ArrayList<>());
                    arr.add(td);
                }
            }
        }
        return arr;
    }

    private static List<TreeDzzDesc> getDzzTD(String parentId, TreeDzzDesc parentObj, List<Map<String, Object>> data, String parentColName, String idColName , String colLabel, Boolean argIsContainsData, List<String> argSelectIds)
    {
        List<TreeDzzDesc> arr=new ArrayList<>();
        if(data==null || data.size()<1)
            return arr;
        for(int i=0;i<data.size();i++)
        {
            Map cd=data.get(i);
            if(parentId == null || parentId.trim().equals(""))
            {
                if(cd.get(parentColName)==null || cd.get(parentColName).toString().trim().equals(""))
                {
                    TreeDzzDesc td=new TreeDzzDesc();
                    td.setKey(cd.get(idColName)+"");
                    td.setTitle(cd.get(colLabel).toString());
                    if(argSelectIds!=null && argSelectIds.contains(cd.get(idColName)+""))
                        td.setCheckbox(true);
                    else
                        td.setCheckbox(false);
                    if(argIsContainsData)
                        td.setData(cd);
                    List<TreeDzzDesc> childrenArray=getDzzTD(cd.get(idColName).toString().trim(),td,data,parentColName,idColName,colLabel,argIsContainsData,argSelectIds);
                    if(childrenArray!=null && childrenArray.size()>0)
                        td.setChildren(childrenArray);
                    else
                        td.setChildren(new ArrayList<>());
                    arr.add(td);
                }
            }
            else
            {
                if(cd.get(parentColName).toString().equals(parentId.toString().trim()))
                {
                    TreeDzzDesc td=new TreeDzzDesc();
                    td.setKey(cd.get(idColName)+"");
                    td.setTitle(cd.get(colLabel).toString());
                    if(argSelectIds!=null && argSelectIds.contains(cd.get(idColName)+""))
                        td.setCheckbox(true);
                    else
                        td.setCheckbox(false);
                    if(argIsContainsData)
                        td.setData(cd);
                    List<TreeDzzDesc> childrenArray=getDzzTD(cd.get(idColName).toString().trim(),td,data,parentColName,idColName,colLabel,argIsContainsData,argSelectIds);
                    if(childrenArray!=null && childrenArray.size()>0)
                        td.setChildren(childrenArray);
                    else
                        td.setChildren(new ArrayList<>());
                    arr.add(td);
                }
            }
        }
        return arr;
    }

    /** *
     * @Desc: 获得树结构的描述对象
     * @Author  zxd
     * @Date   2020-02-04 10:54
     * @Param parentId
     * @Param data
     * @Param parentColName
     * @Param idColName
     * @Param colLabel
     * @Param argIsContainsData
     * @param argSelectIds
     * @return com.unicom.util.tree.TreeDesc
     * @Exception
     */
    public static TreeDesc getTreeDesc(String parentId, List<Map<String, Object>> data, String parentColName, String idColName , String colLabel, Boolean argIsContainsData, List<String> argSelectIds){
        List<TreeDesc> children = getTD(parentId, null, data, parentColName, idColName, colLabel,argIsContainsData,argSelectIds);
        if(AssertValid.isEmpty(children))
            return null;
        return children.get(0);
    }

    /** *
     * @Desc: 获得树结构的描述对象
     * @Author  zxd
     * @Date   2020-02-04 10:54
     * @Param parentId
     * @Param data
     * @Param parentColName
     * @Param idColName
     * @Param colLabel
     * @Param argIsContainsData
     * @param argSelectIds
     * @return com.unicom.util.tree.TreeDesc
     * @Exception
     */
    public static TreeDzzDesc getDzzTreeDesc(String parentId, List<Map<String, Object>> data, String parentColName, String idColName , String colLabel, Boolean argIsContainsData, List<String> argSelectIds){
        List<TreeDzzDesc> children = getDzzTD(parentId, null, data, parentColName, idColName, colLabel,argIsContainsData,argSelectIds);
        if(AssertValid.isEmpty(children))
            return null;
        return children.get(0);
    }
    /** *
     * @Desc: 获得树结构的描述对象
     * @Author  zxd
     * @Date   2020-02-04 10:54
     * @Param parentId
     * @Param data
     * @Param parentColName
     * @Param idColName
     * @Param colLabel
     * @Param argIsContainsData
     * @return com.unicom.util.tree.TreeDesc
     * @Exception
     */
    public static List<TreeDesc> getTreeDescList(String parentId, List<Map<String, Object>> data, String parentColName, String idColName , String colLabel, Boolean argIsContainsData, List<String> argSelectIds){
        List<TreeDesc> children = getTD(parentId, null, data, parentColName, idColName, colLabel,argIsContainsData,argSelectIds);
        return children;
    }
}